#!/usr/bin/env escript
-mode(compile).

% How to make a windows build:
% 1. Install erlang and git for windows
% 2. Add c:\Program Files\erl6.x.x to path
% 3. clone actordb
% 4. build rebar for windows, copy the rebar.cmd file to actordb folder
% 5. rebar get-deps 
% 6. rebar compile (yamerl is not windows compatible, remove all makefiles and rebar will be able to compile it)
% 7. winbuild.bat
% 8. Take erlang installation and copy it to win folder. Remove erl.ini files in bin and erts/bin folders
% 9. Use win folder.

main(_Args) ->
  filelib:ensure_dir("win/ebin/"),
  filelib:ensure_dir("win/priv/"),
  filelib:ensure_dir("win/data/"),
  filelib:ensure_dir("win/etc/"),
  filelib:ensure_dir("win/log/"),
  filelib:ensure_dir("win/licenses/"),
  filelib:ensure_dir("win/extensions/"),
  
  [file:copy(F,"win/ebin/"++filename:basename(F)) || F <- filelib:wildcard("deps/*/ebin/*")],
  [file:copy(F,"win/priv/"++filename:basename(F)) || F <- filelib:wildcard("deps/*/priv/*.dll")],
  
  file:copy("LICENSE","win/licenses/LICENSE"),
  [file:copy(F,"win/licenses/LICENSE-"++hd(tl(filename:split(filename:dirname(F))))) || F <- filelib:wildcard("deps/*/LICENSE")],
  
  file:write_file("win/installdeps.bat",deps()),
  file:write_file("win/actordb.bat",run()),
  file:write_file("win/actordb_console.bat",ctrl()),
  file:write_file("win/stop-background-processes.bat",epmd()),

  os:cmd("escript.exe priv/mkconsole.escript"),
  file:copy("priv/actordb_wxconsole","win/priv/actordb_wxconsole"),

  file:copy("rel/files/vm.args","win/etc/vm.args"),
  file:copy("rel/files/sqlite_extensions_here","win/extensions/sqlite_extensions_here"),
  file:copy("etc/init.sql","win/etc/init.sql"),
  {ok,AppFile} = file:read_file("rel/files/app.config"),
  AppFile1 = re:replace(AppFile,"{{platform_data_dir}}","data",[global]),
  AppFile2 = re:replace(AppFile1,"{{platform_log_dir}}","log",[global]),
  AppFile3 = re:replace(AppFile2,"{{extensions_dir}}","extensions",[global]),
  file:write_file("win/etc/app.config",AppFile3),

  ToolCmd = "cl deps\\actordb_driver\\c_src\\tool.c deps\\actordb_driver\\c_src\\mdb.c deps\\actordb_driver\\c_src\\midl.c deps\\actordb_driver\\c_src\\lz4.c /D_TESTAPP_=1 /DSQLITE_DEFAULT_PAGE_SIZE=4096 /DSQLITE_DEFAULT_WAL_AUTOCHECKPOINT=0 /Ic_src Ws2_32.lib Advapi32.lib",
  os:cmd(ToolCmd),
  file:rename("tool.exe","win/tool.exe"),
  [file:delete(Nm) || Nm <- filelib:wildcard("*.obj")],
  ok.


run() ->
  check("erl.exe") ++
  "start \"\" \"%CD%/erl7.3/bin/werl\" -pa ebin -config etc/app.config -s actordb_core -args_file etc/vm.args\n"++
  "exit\n".

deps() ->
  check("")++
  "start \"\" \"%CD%/erl7.3/vcredist_x64.exe\" /q /norestart\n"++
  "exit\n".

epmd() ->
  check("")++
  "\"%CD%/erl7.3/erts-7.3/bin/epmd\" -kill\n"++
  "exit\n".

ctrl() ->
  check("escript.exe") ++
  %"%CD%/erl7.2.1/bin/escript.exe priv/actordb_console %*\n"++
  "start /min \"\" \"%CD%/erl7.3/bin/escript.exe\" priv/actordb_wxconsole %*\n".
  %"pause".  

check(_Name) ->
  "@echo off\n"++
  "setlocal\n".

% Used to clean up erlang folder.
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".erl"].
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".lib"].
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".pdb"].
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".hrl"].      
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".html"].
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".gif"]. 
%[file:delete(Nm) || Nm <- filelib:wildcard("**"), filename:extension(Nm) == ".pdf"].
